package design.pattern.Lu08MediatorPattern;

import java.util.Random;

/**
 * @program JavaBase
 * @description: 中介者模式
 *  用一个中介对象封装一系列的对象 交互，中介者使各对象不需要显示地相互作用，从而使其耦合松散，而且可以独立地改变它 们之间的交互。
 * @author: zhanglu
 * @create: 2020-03-29 17:54:00
 */
public class Lu08MediatorPattern01 {

    public static void main(String[] args) {
        AbstractMediator mediator = new Mediator();
        //采购人员采购电脑
        System.out.println("------采购人员采购电脑--------");
        Purchase01 purchase = new Purchase01(mediator);
        purchase.buyIBMcomputer(100);
        //销售人员销售电脑
        System.out.println("\n------销售人员销售电脑--------");
        Sale01 sale = new Sale01(mediator);
        sale.sellIBMComputer(1);
        //库房管理人员管理库存
        System.out.println("\n------库房管理人员清库处理--------");
        Stock01 stock = new Stock01(mediator);
        stock.clearStock();
    }

}

//只负责处理自己的活动（行为），与自己无关的活动就丢给中介者处理
abstract class AbstractMediator{

    protected Purchase01 purchase;
    protected Stock01 stock;
    protected Sale01 sale;

    public AbstractMediator(){
        this.purchase = new Purchase01(this);
        this.stock = new Stock01(this);
        this.sale = new Sale01(this);
    }

    protected abstract void execute(String command, Object... objects);

}

class Mediator extends AbstractMediator{

    @Override
    protected void execute(String command, Object... objects) {
        if(command.equals("purchase.buy")){ //采购电脑
            this.buyComputer((Integer)objects[0]);
        }else if(command.equals("sale.sell")){ //销售电脑
            this.sellComputer((Integer)objects[0]);
        }else if(command.equals("sale.offsell")){ //折价销售
            this.offSell();
        }else if(command.equals("stock.clear")){ //清仓处理
            this.clearStock();
        }
    }

    //采购电脑
    private void buyComputer(int number){
        int saleStatus = super.sale.getSaleStatus();
        if(saleStatus>80){ //销售情况良好
            // System.out.println("采购IBM电脑:"+number + "台");
            super.stock.increase(number); }else{ //销售情况不好
            int buyNumber = number/2; //折半采购
            System.out.println("采购IBM电脑："+buyNumber+ "台");
        }
    }

    //销售电脑
    private void sellComputer(int number){
        if(super.stock.getStockNumber()<number){ //库存数量不够销售
            super.purchase.buyIBMcomputer(number);
        }
        super.stock.decrease(number);
    }

    //折价销售电脑
    private void offSell(){
        System.out.println("折价销售IBM电脑"+stock.getStockNumber()+"台");
    }

    //清仓处理
    private void clearStock(){
        //要求清仓销售
        super.sale.offSale();
        //要求采购人员不要采购
        super.purchase.refuseBuyIBM();
    }

}

abstract class AbstractColleague{

    protected AbstractMediator mediator;

    public AbstractColleague(AbstractMediator abstractMediator){
        this.mediator = abstractMediator;
    }

}

class Purchase01 extends AbstractColleague{

    public Purchase01(AbstractMediator abstractMediator) {
        super(abstractMediator);
    }

    //采购IBM电脑
    public void buyIBMcomputer(int number){
        super.mediator.execute("purchase.buy", number);
    }

    //不再采购IBM电脑
    public void refuseBuyIBM(){
        System.out.println("不再采购IBM电脑");
    }

}

class Stock01 extends AbstractColleague{

    public Stock01(AbstractMediator abstractMediator) {
        super(abstractMediator);
    }

    //刚开始有100台电脑
    private static int COMPUTER_NUMBER =100;

    //库存增加
    public void increase(int number) {
        COMPUTER_NUMBER = COMPUTER_NUMBER + number;
        System.out.println("库存数量为："+COMPUTER_NUMBER);
    }

    //库存降低
    public void decrease(int number){
        COMPUTER_NUMBER = COMPUTER_NUMBER - number;
        System.out.println("库存数量为："+COMPUTER_NUMBER);
    }

    //获得库存数量
    public int getStockNumber(){
        return COMPUTER_NUMBER;
    }

    //存货压力大了，就要通知采购人员不要采购，销售人员要尽快销售
    public void clearStock(){
        super.mediator.execute("stock.clear");
    }

}

class Sale01 extends AbstractColleague{

    public Sale01(AbstractMediator abstractMediator) {
        super(abstractMediator);
    }

    //销售IBM电脑
    public void sellIBMComputer(int number){
        Stock stock = new Stock();
        Purchase purchase = new Purchase();
        if(stock.getStockNumber()<number){ //库存数量不够销售
            purchase.buyIBMcomputer(number);
        }
        System.out.println("销售IBM电脑"+number+"台");
        stock.decrease(number);
    }

    //反馈销售情况，0～100变化，0代表根本就没人买，100代表非常畅销，出一个卖一个
    public int getSaleStatus(){
        Random rand = new Random(System.currentTimeMillis());
        int saleStatus = rand.nextInt(100);
        System.out.println("IBM电脑的销售情况为："+saleStatus);
        return saleStatus;
    }

    //折价处理
    public void offSale(){ //库房有多少卖多少
        super.mediator.execute("sale.offsell");
    }

}
